clc;
clear all;
% Starting Time：2020.11.19
% Finish Time：2020.11.23

% AT Target, contain Local Totally Situation
% Scheme1：Gothrough Scheme （Compare）
% Scheme2：Golden Hungarian Scheme（Propose）
% Scheme3：RLT Round Scheme （Propose）
% Scheme4：Greedy Channel[Users perspective] Scheme （Propose）
% Scheme5：Random Scheme （Compare）

%% Parameter settings
%%% Environment %%%
B = 2*10^6;
noise_p = 10^(-9);
% Helpers Distribution: 0~4 m 、  Users Distribution: 4~7 m
r_EN_large = 7;
r_EN_small = 4;

%%% Energy Node %%%
eff = 0.67;
P_w = 3;

%%% User %%%
num_u = 2;
cpu = 10^(-28);

%% Algorithm
num_h_exp = 2:2:10;

% % Scheme1：Gothrough Scheme
% Gothrough_final_exp = zeros(1,length(num_h_exp));
% % Scheme2：Golden Hungarian Scheme
% GoldenHungarian_final_exp = zeros(1,length(num_h_exp));
% Scheme3：RLT Round Scheme
RLTRound_final_exp = zeros(1,length(num_h_exp));
% % Scheme4：Greedy Channel[Users perspective] Scheme
% GreedyChaU_final_exp = zeros(1,length(num_h_exp));

fail_num = 0;
% outter condition
%for i_main = 1:length(num_h_exp)
    i_main = 5;
    num_h = num_h_exp(i_main);
    
    % Monte Calor
    run_total = 700;
%     Gothrough_final_run = zeros(1,run_total);
%     GoldenHungarian_final_run = zeros(1,run_total);
    RLTRound_final_run = zeros(1,run_total);
%     GreedyChaU_final_run = zeros(1,run_total);
    
    for run = 1:run_total
        disp(i_main);
        disp(run);
        bit_to_cycle = randi([500 800],1,num_u);
        Data = randi([20*10^3 30*10^3],1,num_u);
        [d_wu,d_wh,d_uh] = situation(num_u,num_h,r_EN_large,r_EN_small);
        % EN到Users信道
        h_large_wu = (10^(-3))*(d_wu.^(-3));
        h_wu = zeros(1,num_u);
        for n = 1:num_u
            h_wu(n) = h_large_wu(n)*Rayleighchannel();
        end
        H_wu = h_wu;
        % EN到Helpers信道
        h_large_wh = (10^(-3))*(d_wh.^(-3));
        h_wh = zeros(1,num_h);
        for m = 1:num_h
            h_wh(m) = h_large_wh(m)*Rayleighchannel();
        end
        H_wh = h_wh;
        % Users到Helpers信道
        h_large_uh = (10^(-3))*(d_uh.^(-3));
        h_uh = zeros(num_u,num_h);
        for n = 1:num_u
            for m = 1:num_h
                h_uh(n,m) = h_large_uh(n,m)*Rayleighchannel();
            end
        end
        H_uh = h_uh./noise_p;
        
%         disp('Scheme1：Gothrough Scheme');
%         disp(i_main);
%         disp(run);
%         [Gothrough_FinalResult] = GoThrough_CL(B,num_u,num_h,H_wu,H_wh,H_uh,eff,P_w,bit_to_cycle,cpu,Data);
%         if isinf(Gothrough_FinalResult)||isnan(Gothrough_FinalResult)
%             Gothrough_final_run(run) = 0;
%         else
%             Gothrough_final_run(run) = Gothrough_FinalResult/num_u; % Average Time
%         end
%         
%         disp('Scheme2：Golden Hungarian Scheme');
%         disp(i_main);
%         disp(run);
%         [GoldenHun_FinalResult] = GoldenHungarian_CL(B,num_u,num_h,H_wu,H_wh,H_uh,eff,P_w,bit_to_cycle,cpu,Data);
%         if isinf(GoldenHun_FinalResult)||isnan(GoldenHun_FinalResult)
%             GoldenHungarian_final_run(run) = 0;
%         else
%             GoldenHungarian_final_run(run) = GoldenHun_FinalResult/num_u; % Average Time
%         end
%         
%         disp('Scheme4：Greedy Channel[Users perspective] Scheme');
%         disp(i_main);
%         disp(run);
%         [GreedyChannel_FinalResult] = GreedyChannel_CL(B,num_u,num_h,H_wu,H_wh,H_uh,eff,P_w,bit_to_cycle,cpu,Data);
%         if isinf(GreedyChannel_FinalResult)||isnan(GreedyChannel_FinalResult)
%             GreedyChaU_final_run(run) = 0;
%         else
%             GreedyChaU_final_run(run) = GreedyChannel_FinalResult/num_u; % Average Time
%         end
        
        disp('Scheme3：RLT Round Scheme');
        disp(i_main);
        disp(run);
        [result_relax,a_pairing_relax] = RLTRound_CL_sub1(B,num_u,num_h,H_wu,H_wh,H_uh,eff,P_w,bit_to_cycle,cpu,Data);
        if isinf(result_relax)||isnan(result_relax)
%             GoldenHungarian_final_run(run) = 0;
%             Gothrough_final_run(run) = 0;
            RLTRound_final_run(run) = 0;
%             GreedyChaU_final_run(run) = 0;
            fail_num = fail_num + 1;
        else
            [RLTRound_FinalResult] = RLTRound_CL_sub2(a_pairing_relax,B,num_u,num_h,H_wu,H_wh,H_uh,eff,P_w,bit_to_cycle,cpu,Data);
            RLTRound_final_run(run) = RLTRound_FinalResult/num_u; % Average Time
        end
        
    end
%     Gothrough_final_exp(i_main) = sum(Gothrough_final_run(:))/run_total;
%     GoldenHungarian_final_exp(i_main) = sum(GoldenHungarian_final_run(:))/run_total;
     RLTRound_final_exp(i_main) = sum(RLTRound_final_run(:))/run_total;
%     GreedyChaU_final_exp(i_main) = sum(GreedyChaU_final_run(:))/run_total;
%end

figure(1);
% plot(num_h_exp(:),Gothrough_final_exp(:),'-hr','LineWidth',1,'MarkerSize',7); grid on; hold on;
% plot(num_h_exp(:),GoldenHungarian_final_exp(:),'--v','Color',[0.93 0.57 0.13],'LineWidth',1,'MarkerSize',7); grid on; hold on;
plot(num_h_exp(:),RLTRound_final_exp(:),'--ob','LineWidth',1,'MarkerSize',7); grid on; hold on;
% plot(num_h_exp(:),GreedyChaU_final_exp(:),'--p','Color',[0.50 0.16 0.16],'LineWidth',1,'MarkerSize',7); grid on; hold on;
% legend('暴力搜索方案','黄金匈牙利方案','基于RLT的配对方案','基于信道条件的配对方案');
xlabel('空闲用户数量');
ylabel('系统平均任务完成时延/s');